package com.tfswue.ershoujiaoyi.controller;

import com.tfswue.ershoujiaoyi.entity.Review;
import com.tfswue.ershoujiaoyi.service.ReviewService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "评价管理", description = "商品评价的添加、查询、修改和删除")
@RestController
@RequestMapping("/api/review")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    @Operation(summary = "添加商品评价", description = "用户对商品进行评价，添加评论和评分")
    @PostMapping("/add")
    public Review addReview(@RequestBody Review review) {
        return reviewService.addReview(review);
    }

    @Operation(summary = "查询商品评价列表", description = "根据商品ID查询该商品的所有评价")
    @GetMapping("/list")
    public List<Review> listByProduct(@Parameter(description = "商品ID", required = true) @RequestParam Long productId) {
        return reviewService.listByProduct(productId);
    }

    @Operation(summary = "查询所有评价", description = "查询全部评价记录")
    @GetMapping("/all")
    public List<Review> listAll() {
        return reviewService.listAll();
    }

    @Operation(summary = "根据ID查询评价", description = "根据评价ID查询评价内容")
    @GetMapping("/{id}")
    public Review getById(@Parameter(description = "评价ID", required = true) @PathVariable Long id) {
        return reviewService.getById(id);
    }

    @Operation(summary = "修改评价内容", description = "根据评价ID修改评价内容和分数")
    @PutMapping("/update")
    public Review update(@RequestBody Review review) {
        return reviewService.update(review);
    }

    @Operation(summary = "删除评价", description = "根据评价ID删除评价")
    @DeleteMapping("/delete/{id}")
    public String delete(@Parameter(description = "评价ID", required = true) @PathVariable Long id) {
        boolean success = reviewService.delete(id);
        return success ? "删除成功" : "删除失败";
    }
}
